package com.example.homework.recycler

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class MyDecoration(var mContext:Context) : RecyclerView.ItemDecoration() {

    val DIVIDER_HEIGHT = 4
    val HEADER_HEIGHT = dp2px(mContext,30)
    val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    init {
        mPaint.isDither = true
        mPaint.color = Color.YELLOW
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {



    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.adapter is RecyclerActivity.MyAdapter){
            val adapter = parent.adapter as RecyclerActivity.MyAdapter
            val position = parent.getChildAdapterPosition(view)
            val isHeader = adapter.isGroupHeader(position)
            outRect.top = if (isHeader) HEADER_HEIGHT else DIVIDER_HEIGHT
        }
    }

    fun dp2px(context: Context,dp:Int):Int{
        val scale = context.resources.displayMetrics.density
        return (dp*scale+0.5f).toInt()
    }
}